package com.miyake.client.datamanage;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.tools.ImageListDialog;
import com.miyake.client.tools.ItemListDialog;
import com.miyake.client.tools.PlotListDialog;
import com.miyake.client.tools.SmallButton;

public class PaperPage extends Page {
	private Widget toolBar;
	private VerticalPanel contentPanel;
	
	public PaperPage(NodeProperty node, VerticalPanel panel) {
		super(node, panel);
	}

	@Override
	protected Widget getContent(final NodeProperty node, final DataServiceAsync service) {
		contentPanel = new VerticalPanel();
		contentPanel.setWidth("100%");
		if (canEdit()) {
			contentPanel.add(createToolBar(node, service));
			contentPanel.add(new SecurityLevelWidget(node, service));
		}
		
		/*
		ShowSubItems show = new ShowSubItems(node,contentPanel, new NodeUpdateHandler() {
			@Override
			public void onNodeUpdate(String nodeKey) {
				fireNodeUpdateEvent(nodeKey);
			}		
		});
		*/
		
		ShowSubItems show = new ShowSubItems(node,contentPanel, this.updateHandler);
		show.update();	

		return contentPanel;
	}

	private HorizontalPanel createToolBar(final NodeProperty node,
			final DataServiceAsync service) {
		
		HorizontalPanel toolBar = new HorizontalPanel();
		toolBar.add(new SmallButton("Text", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				createHandler.onNewText();
			}
		}));
		
		toolBar.add(new SmallButton("Import Chart", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				showListDialog(node, service, new PlotListDialog(node.getParentKey()));
			}
		}));

		toolBar.add(new SmallButton("Import Image", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				showListDialog(node, service, new ImageListDialog(node.getParentKey()));
			}
		}));
		return toolBar;
	}

	protected void showListDialog(final NodeProperty node,
			final DataServiceAsync service, final ItemListDialog dlg) {
		dlg.center();
		dlg.addCloseHandler(new CloseHandler<PopupPanel>() {
			@Override
			public void onClose(CloseEvent<PopupPanel> event) {
				if (!event.isAutoClosed()) {
					Integer type = null;
					if ( dlg instanceof PlotListDialog) {
						type = NodeProperty.PLOT;
					}
					else if (dlg instanceof ImageListDialog) {
						type = NodeProperty.IMAGE;
					}
				
					createAlias(dlg.getSelectedKey(), service, node, type);
				}
			}
		});
	}

	protected void createAlias(String selectedKey, DataServiceAsync service, final NodeProperty node, Integer type) {
		service.createAlias(node.getKey(), selectedKey, type, new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(String result) {
				fireNodeUpdateEvent(node.getKey());
			}
		});
	}

	@Override
	public void setSelected(NodeProperty node) {
		if (toolBar != null) {
			this.contentPanel.remove(toolBar);
		}
		toolBar = createToolBar(node, getService());
		int i = 0;
		for (NodeProperty n : this.getNode().getChildren()) {
			if (n.getKey().equals(node.getKey())) {
				break;
			}
			i++;
		}
//		this.getPanel().add(createToolBar(node, getService()));	
		this.contentPanel.insert(toolBar, i+1);	
	}
	
	
}
